﻿using AvalonDock;
using ReflectionStudio.Classes;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MainWindow 
    {
        #region ---------------------PROPERTIES---------------------
        ProcessHelper ProcessHelper = new ProcessHelper(); 
        public Assembly assembly = null; 
        private DocumentContent ActiveDocument
        {
            get;
            set;
        }
        DocumentBase doc; 
        
        string filePath;
        Dictionary<string, object> dicParamater
        {
            get
            {
                return ApplicationBase.dicParamater;
            }
        } 
        #endregion

        #region ---------------------WINDOW EVENTS---------------------
        private void btnUserSetUp_Click(object sender, RoutedEventArgs e)
        {
            ApplicationBase.ReSetSETUP();
            SetUpUserMenu();
        } 
        #endregion

        private DocumentBase DisplayDocument(Type docType, Dictionary<string, object> dicParamater)
        {
            return DocumentFactory.Instance.OpenDocument(docType, new XpsDocumentDataContext(), dicParamater);
        }
 
        public void SetUpUserMenu()
        {
            DataTable umenu = ApplicationBase.UserTable;
            if (umenu == null)
                return;
            Control ct = null;  
            foreach (DataRow dwUMenu in umenu.Rows)
            {
                string com = dwUMenu["COMPANY"].ToString().Trim();
                string proc = dwUMenu["PROCESS"].ToString().Trim();
                string menuitem = dwUMenu["MENUITEM"].ToString().Trim();
                string father = dwUMenu["FATHER"].ToString().Trim(); 
                if (proc == "VISIBLE")
                {
                    ct = (Control)this.FindName(menuitem);
                    if (ct != null)
                    {
                        ct.IsEnabled = true;
                        ct.Visibility = Visibility.Visible;
                    }
                    ct = (Control)this.FindName("t"+menuitem);
                    if (ct != null)
                    {
                        ct.IsEnabled = true;
                        ct.Visibility = Visibility.Visible;
                    }
                }
                if (proc == "MENU")
                {
                    Fluent.RibbonGroupBox cf = (Fluent.RibbonGroupBox)this.FindName(father);
                    if (cf != null)
                    {
                        cf.IsEnabled = true;
                        cf.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        continue;
                    }
                    Fluent.Button cb = new Fluent.Button();
                    cb.Header =Helper.GetGB2312Value(dwUMenu["HEADER"].ToString().Trim());
                    cb.Tag = dwUMenu["TAG"].ToString().Trim();
                    cb.Icon = dwUMenu["ICON"].ToString().Trim();
                    cb.LargeIcon = dwUMenu["LargeIcon"].ToString().Trim();
                    cb.SizeDefinition="Large";
                   
                    cb.Click += new RoutedEventHandler(btnReport_Click);
                    cf.Items.Add(cb);

                    TreeViewItem ctree= (TreeViewItem)this.FindName("t"+father);
                    if (ctree != null)
                    {
                        ctree.IsEnabled = true;
                        ctree.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        continue;
                    }
                    TreeViewItem ctx = new TreeViewItem();
                    ctx.Header = dwUMenu["HEADER"].ToString().Trim();
                    ctx.Tag = dwUMenu["TAG"].ToString().Trim();
                    ctree.Items.Add(ctx);
                    //TreeViewItem
                }
            }
        }

        public void ActivateReportByDocType(string doctype)
        {
            Type docType = ApplicationBase.GetDoc(doctype);
            if (docType == null)
            {
                //MessageBox.Show("怎么会发生这种没有技术含量的事情呢!");
                return;
            }
            DisplayDocument(docType, dicParamater);
        } 
        private void treeViewReport_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            try
            {
                if (((TreeViewItem)e.NewValue).Tag == null)
                    return;
                string doctype = ((TreeViewItem)e.NewValue).Tag.ToString();
                ActivateReportByDocType(doctype);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            Button sd = sender as Button;
            string doctype = sd.Tag.ToString();
            ActivateReportByDocType(doctype);
        }
    }
}
